package com.tjhk.java.tech.thread.pool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author ：little grape
 * @description：TODO
 * @date ：2023/4/18 22:40
 */
public class TimeAbort {
    private volatile boolean restart = true;

    public TimeAbort(double t, String msg) {
        CompletableFuture.runAsync(()->{
            while (restart){
                restart = false;
                try {
                    TimeUnit.MILLISECONDS
                            .sleep((int)(1000 * t));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(msg);
            System.exit(0);
        });
    }

    public TimeAbort(double t){
        this(t,"TimeAbort "+t);
    }
    public void restart(){
        restart = true;
    }
    public static void main(String[] args) {
        new TimeAbort(1);
    }
}
